/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import entity.HashTag;
import entity.Tweet;
import entity.Users;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import sessionbeans.HashTagFacade;
import sessionbeans.TweetFacade;
import sessionbeans.UsersFacade;

/**
 *
 * @author Axel
 */
@ManagedBean(name="TweetBean")
@RequestScoped
public class TweetBean {

    /** Creates a new instance of Tweet */

    
    @EJB
    private TweetFacade tf;

    @EJB
    private HashTagFacade htf;

    @EJB
    private UsersFacade uf;
    
    @ManagedProperty(value="#{userBean}") // #{user} is the managed bean name
    private sessionUserBean userBean;

    @ManagedProperty(value="#{userTempBean}") // #{user} is the managed bean name
    private sessionUserTempBean userTempBean;
    
    private String message = "";
    
    private Long id;

    public TweetBean() {
    }

    public String tweetMessage(Tweet aTweet){
        String res;
        this.message = aTweet.getUser().getName()+" "+aTweet.getMessage();
        res = tweet();
        return res;
    }
    
    public String tweet(){
        String result;
        Tweet aTweet = new Tweet();
        aTweet.setMessage(this.message);
        aTweet.setUser(this.getUserBean().getUser());
        String[] array = aTweet.getMessage().split(" ");
        for(int i = 0; i < array.length ; i++){
            if(array[i].startsWith("#")){
                HashTag AHashTag = new HashTag();
                AHashTag.setLabel(array[i]);
                if(htf.getByName(array[i]) == null){
                    htf.create(AHashTag);
                    aTweet.getTags().add(AHashTag);
                }else{
                    aTweet.getTags().add(htf.getByName(array[i]));
                }
            }
        }
        tf.create(aTweet);
        if (this.getUserBean().getUser() != null){
            this.getUserBean().getUser().getTweets().add(aTweet);
            this.getUserTempBean().setUser(this.getUserBean().getUser());
            result = "profile";
        }else{
            result = "error";
        }
            
        return result;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the userBean
     */
    public sessionUserBean getUserBean() {
        return userBean;
    }

    /**
     * @param userBean the userBean to set
     */
    public void setUserBean(sessionUserBean userBean) {
        this.userBean = userBean;
    }

    /**
     * @return the userTempBean
     */
    public sessionUserTempBean getUserTempBean() {
        return userTempBean;
    }

    /**
     * @param userTempBean the userTempBean to set
     */
    public void setUserTempBean(sessionUserTempBean userTempBean) {
        this.userTempBean = userTempBean;
    }



}
